/** 
 * @ClassName:     MtDbReader.java 
 * @Description:   TODO(用一句话描述该文件做什么)  
 * @author         wangzhe  
 * @Date           2019年7月24日 下午2:33:20  
 */  

package com.huitone.smspfm.oldclient.asclient.send;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.huitone.smspfm.oldclient.model.DbSmsConf;
import org.springframework.beans.factory.annotation.Autowired;

import com.huitone.smspfm.context.utils.ContextUtils;
import com.huitone.smspfm.oldclient.app.AppStaticConfigs;
import com.huitone.smspfm.oldclient.dao.SmsSendBackUpDao;
import com.huitone.smspfm.oldclient.dao.SmsSendDao;
import com.huitone.smspfm.oldclient.dao.entity.SmsSend;
import com.huitone.smspfm.oldclient.logger.ExtLogger;


public class MtDbReader {
	
	
	private final static ExtLogger logger = ExtLogger.create(MtDbReader.class);
	
	private boolean backupFile = false;
	
	private String backupTableName ="CLIENT_SMSSEND_BACKUP_";
	
	
	
	public List<SmsSend> getSmsSend() {
		
			synchronized (MtDbReader.class) {
				List<SmsSend> result = new ArrayList<SmsSend>();
				Integer limit = AppStaticConfigs.smsSendLimit;
				Integer size = 0;
				long current = System.currentTimeMillis();
//				List<String> tables = AppStaticConfigs.smsSendTables;
				List<DbSmsConf> dbSmsConfList = AppStaticConfigs.dbSmsConfList;
//				for(int table =0;table<tables.size();table++){
				for(DbSmsConf dbSmsConf : dbSmsConfList){
					try {
						//对表进行时段过滤   starttime, endtime
						if (dbSmsConf.forbitTime(current)) {
							logger.info(dbSmsConf.getTable()+"处理禁发时间, 不进行短信读取！");
							continue;
						}
						SmsSendDao smsSendDao = ContextUtils.getBean2(SmsSendDao.class);
//						smsSendDao.setTableName(tables.get(table));
						smsSendDao.setTableName(dbSmsConf.getTable());
						List<SmsSend> smsSends = smsSendDao.getAllObj(limit-size);
						int[] deleteObj = smsSendDao.deleteObj(smsSends);
						for(int i = 0; i<deleteObj.length;i++){
							if(deleteObj[i]!=1&&deleteObj[i]!=Statement.SUCCESS_NO_INFO){
								logger.error(getClass().getSimpleName()+"从数据库读取的此条数据未删除："+smsSends.get(i));
							}
						}
						size += smsSends.size();
						result.addAll(smsSends);
						if (size>=limit) {
							return result;
						}
//						if(table==tables.size()-1){
//							return result;
//						}
						
					} catch (Exception e) {
						logger.error(getClass().getSimpleName()+"从数据库读取的发送表数据并删除过程出错："+dbSmsConf.parseToJson());
						e.printStackTrace();
					}
					
				}
				return result;
			}
	}
	
	public Boolean backupSms(List<SmsSend> smsSends) {
		synchronized (MtDbReader.class) {
			if(backupFile){
				// 获得当前时间
				Date getDate = Calendar.getInstance().getTime();
				String dateStr = new SimpleDateFormat("yyyyMMdd").format(getDate);
				String tableName = backupTableName+ dateStr;
				SmsSendBackUpDao smsSendBackUpDao = ContextUtils.getBean2(SmsSendBackUpDao.class);
				try {
					smsSendBackUpDao.setTableName(tableName);
					boolean exists = smsSendBackUpDao.isTableExists(tableName);
					
					if(!exists){
						String tableSql ="create table " +tableName+" ( SERIALNO VARCHAR2(50) , CLIENTNAME VARCHAR2(20), SENDPORT VARCHAR2(20), MOBILENO VARCHAR2(20), CONTENT VARCHAR2(300), WAPURL VARCHAR2(300), MESGTYPE NUMBER, SRR NUMBER )";
						boolean createTable = smsSendBackUpDao.createTable(tableSql);
						if (!createTable) {
							logger.error(getClass().getSimpleName()+"创建备份表失败："+tableName);
							return false;
						}
					}
				} catch (Exception e) {
					logger.error(getClass().getSimpleName()+"创建备份表失败："+tableName);
					e.printStackTrace();
				}
				
				
				try {
					int[] insert = smsSendBackUpDao.insertBackUpSms(smsSends);
					for(int i = 0; i<insert.length;i++){
						if(insert[i]!=1&&insert[i]!=Statement.SUCCESS_NO_INFO){
							logger.error(getClass().getSimpleName()+"此条数据未备份成功："+smsSends.get(i));
							return false;
						}
					}
				} catch (Exception e) {
					logger.error(getClass().getSimpleName()+"备份数据时发生异常，失败："+smsSends);
					e.printStackTrace();
				}
				
				return true;
			}else {
				return true;
			}
		}
	}
	
	
	

	public boolean isBackupFile() {
		return backupFile;
	}

	public void setBackupFile(boolean backupFile) {
		this.backupFile = backupFile;
	}

}
